<template>
  <div class="app-container" >

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleSelect"
        >预测</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >智能建户</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="noDoorList" >
      <el-table-column label="小区" align="center" prop="areaName" />
      <el-table-column label="楼宇" align="center" prop="groupName" />
      <el-table-column label="单元" align="center" prop="unitName" />
      <el-table-column label="户" align="center" prop="doorName" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 未建户对话框 -->
    <el-dialog :title="noDoorTitle" :visible.sync="noDoorOpen" width="450px" append-to-body>
      <el-form ref="noDoorForm" :model="noDoorForm" :rules="rules" label-width="80px">
        <el-form-item label="小区" prop="areaName">
          <el-select v-model="noDoorForm.areaName" placeholder="请输入小区" clearable :style="{width: '100%'}" @change="getGroup(noDoorForm.areaName)">
            <el-option v-for="(item, index) in areaIdOptions" :key="index" :label="item.areaName"
                       :value="item.areaName" :disabled="item.disabled" ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="楼宇" prop="groupName">
          <el-select v-model="noDoorForm.groupName" placeholder="请输入楼宇" clearable :style="{width: '100%'}" @change="getUnit(noDoorForm.areaName,noDoorForm.groupName)">
            <el-option v-for="(item, index) in groupIdOptions" :key="index" :label="item.groupName"
                       :value="item.groupName" :disabled="item.disabled" ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="单元" prop="unitName">
          <el-select v-model="noDoorForm.unitName" placeholder="请输入单元" clearable :style="{width: '100%'}" >
            <el-option v-for="(item, index) in unitNameOptions" :key="index" :label="item.unitName"
                       :value="item.unitName" :disabled="item.disabled" ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="noDoorSubmit">确 定</el-button>
        <el-button @click="noDoorCancel">取 消</el-button>
      </div>
    </el-dialog>




    <!-- 智能建户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="450px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="小区" prop="areaName">
          <el-select v-model="form.areaName" placeholder="请输入小区" clearable :style="{width: '100%'}" @change="getGroup(form.areaName)">
            <el-option v-for="(item, index) in areaIdOptions" :key="index" :label="item.areaName"
                       :value="item.areaName" :disabled="item.disabled" ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="楼宇" prop="groupName">
          <el-select v-model="form.groupName" placeholder="请输入楼宇" clearable :style="{width: '100%'}" @change="getUnit(form.areaName,form.groupName)">
            <el-option v-for="(item, index) in groupIdOptions" :key="index" :label="item.groupName"
                       :value="item.groupName" :disabled="item.disabled" ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="单元" prop="unitName">
          <el-select v-model="form.unitName" placeholder="请输入单元" clearable :style="{width: '100%'}" >
            <el-option v-for="(item, index) in unitNameOptions" :key="index" :label="item.unitName"
                       :value="item.unitName" :disabled="item.disabled" ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始层数" prop="startFloorNumber">
          <el-input v-model="form.startFloorNumber" placeholder="请输入开始层数" :style="{width: '100%'}" />
        </el-form-item>
        <el-form-item label="终止层数" prop="offFloorNumber">
          <el-input v-model="form.offFloorNumber" placeholder="请输入终止层数" :style="{width: '100%'}" />
        </el-form-item>
        <el-form-item label="选取户型">
          <el-select v-model="form.doorNumber" placeholder="请选择" :style="{width: '100%'}" >
            <el-option
              v-for="dict in dict.type.HOUSE017"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="面积" prop="space">
          <el-input v-model="form.space" placeholder="请输入面积" />
        </el-form-item>
        <el-form-item label="用途" prop="purposeId">
          <el-select v-model="form.purposeId" placeholder="请输入用途" clearable :style="{width: '100%'}" >
            <el-option v-for="(item, index) in purposeOptions" :key="index" :label="item.purposeName"
                       :value="item.purposeId" :disabled="item.disabled" ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="收费标准" prop="price">
          <el-select v-model="form.price" placeholder="请输入收费标准" clearable :style="{width: '100%'}" :disabled="noWrite">
            <el-option v-for="(item, index) in feesOptions" :key="index" :label="item.price"
                       :value="item.price" :disabled="item.disabled" ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {getAreaInfo, getGroupInfo, getUnitInfo, getPurposeInfo, getFeesInfo} from "@/api/house/door";
import { listNoDoor, addNoDoor, addDoors,} from "@/api/house/noDoorQuery";


export default {
  name: "NoDoor",
  dicts: ['HOUSE017'],
  data() {
    return {
      visible: false,
      left: 0,
      top: 0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 户管理表格数据
      noDoorList: [],
      // 弹出层标题
      title: "",
      noDoorTitle: "",
      // 是否显示弹出层
      open: false,
      noDoorOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        areaName: null,
        groupName: null,
        unitName: null,
        doorName: null,
        houseId: null,
        purposeId: null,
        userName: null,
      },
      areaIdOptions:[{}],
      groupIdOptions:[{}],
      unitNameOptions:[{}],
      purposeOptions:[{}],
      feesOptions:[{}],
      // 表单参数
      form: {},
      noDoorForm: {},
      // 表单校验
      rules: {
        areaName: [
          { required: true, message: "小区不能为空", trigger: "change" }
        ],
        groupName: [
          { required: true, message: "楼宇不能为空", trigger: "change" }
        ],
        unitName: [
          { required: true, message: "单元不能为空", trigger: "change" }
        ],
        doorName: [
          { required: true, message: "户名称不能为空", trigger: "blur" }
        ],
        purposeId: [
          { required: true, message: "用途不能为空", trigger: "blur" }
        ],
        space: [
          { required: true, message: "面积不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "收费标准不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.areaSelect();
    this.purposeSelect();
    this.feesSelect();
  },
  methods: {
    areaSelect(){
      getAreaInfo().then(response => {
        this.areaIdOptions = response.rows;
      });
    },
    getGroup(areaName){
      getGroupInfo(areaName).then(response => {
        this.groupIdOptions = response.rows;
      });
    },
    getUnit(areaName,groupName){
      getUnitInfo(areaName,groupName).then(response => {
        this.unitNameOptions = response.rows;
      });
    },
    purposeSelect(){
      getPurposeInfo().then(response => {
        this.purposeOptions = response.rows;
      });
    },
    feesSelect(){
      getFeesInfo().then(response => {
        this.feesOptions = response.rows;
      });
    },
    /** 查询户管理列表 */
    getList() {
      this.loading = true;
      listNoDoor().then(response => {
        this.noDoorList = response.rows;
        this.loading = false;
        this.areaSelect();
      });
    },
    // 取消按钮
    noDoorCancel() {
      this.noDoorOpen = false;
      this.reset();
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        doorId: null,
        areaName: null,
        groupName: null,
        unitName: null,
        doorName: null,
        houseId: null,
        space: null,
        spaceSts: null,
        purposeId: null,
        userName: null,
        tel: null,
        papersCode: null,
        budgetMoney: null,
        costMoney: null,
        costDate: null,
        costSts: null,
        balance: null,
        rateType: null,
        rateMoney: null,
        rateDate: null,
        creater: null,
        createDate: null,
        remark: null
      };
      this.resetForm("form");
      this.resetForm("noDoorForm");
    },
    /** 新增按钮操作 */
    handleSelect() {
      this.reset();
      this.noDoorOpen = true;
      this.noDoorTitle = "未建户查询";
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "智能建户";
    },

    close() {
      this.reset();
      this.visible = false
    },
    /** 提交按钮 */
    noDoorSubmit() {
      this.$refs["noDoorForm"].validate(valid => {
        if (valid) {
          addNoDoor(this.noDoorForm).then(response => {
            this.noDoorOpen = false;
            this.loading = false;
            this.getList()
            this.areaSelect()
          });
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addDoors(this.form).then(response => {
            this.open = false;
            this.loading = false;
            this.areaSelect()
          });
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('/house/noDoor/export', {
      }, `door_${new Date().getTime()}.xlsx`)
    },


  }
};
</script>
